// page_two/qpqzaddsheb/qpqzaddsheb.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '设备报备',
    buttonClicked: false,
    sbxh: '',
    ssqy: '',
    scdate: '请选择生产日期',
    bfdate: '请选择报废日期',
    scszz: '',
    wuscszz: '',
    scxkz: '',
    wuscxkz: '',
    jcbg: '',
    wujcbg: '',
    hgz: '',
    wuhgz: '',
    cgsz: '',
    wucgsz: '',
  },
  // 设备型号
  sbxh(e) {
    this.setData({
      sbxh: e.detail.value
    })
  },
  // 所属企业
  ssqy(e) {
    this.setData({
      ssqy: e.detail.value
    })
  },
  // 生产日期
  scdate: function (e) {
    this.setData({
      scdate: e.detail.value
    })
  },
  // 报废日期
  bfdate: function (e) {
    if (new Date(e.detail.value).getTime() < Date.parse(new Date())) {
      wx.showToast({
        title: '报废日期需晚于当前日期',
        icon: 'none'
      })
      this.setData({
        bfdate: '请选择报废日期'
      })
    } else {
      this.setData({
        bfdate: e.detail.value
      })
    }
  },
  // 上传
  chuan(e) {
    var aaa = e.currentTarget.dataset.aaa
    var that = this;
    //选取图片
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        //生厂商资质
        if (aaa == 5) {
          that.setData({
            scszz: urls,
            wuscszz: data.imgUrl
          })
        }
        // 生厂许可证
        if (aaa == 6) {
          that.setData({
            scxkz: urls,
            wuscxkz: data.imgUrl
          })
        }
        // 检查报告
        if (aaa == 7) {
          that.setData({
            jcbg: urls,
            wujcbg: data.imgUrl
          })
        }
        // 合格证
        if (aaa == 8) {
          that.setData({
            hgz: urls,
            wuhgz: data.imgUrl
          })
        }
        // 采购索证
        if (aaa == 9) {
          that.setData({
            cgsz: urls,
            wucgsz: data.imgUrl
          })
        }
      })
    })
  },
  showModal(e) {
    var aaa = e.currentTarget.dataset.aaa
    var img = e.currentTarget.dataset.img
    let index = e.currentTarget.id
    this.setData({
      fdaaa: aaa,
      fdimg: img,
      otherxkz_index: index
    })
    let {
      type
    } = e.currentTarget.dataset, va = '';
    if (type == 's2') {
      va = "s2";
    }
    this.setData({
      [va]: true
    })
  },
  confirmbtn(e) {
    console.log(e)
    console.log('点击了从新上传！')
    var aaa = this.data.fdaaa
    var that = this;
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        //生厂商资质
        if (aaa == 5) {
          that.setData({
            scszz: urls,
            wuscszz: data.imgUrl
          })
        }
        // 生产许可证
        if (aaa == 6) {
          that.setData({
            scxkz: urls,
            wuscxkz: data.imgUrl
          })
        }
        // 检查报告
        if (aaa == 7) {
          that.setData({
            jcbg: urls,
            wujcbg: data.imgUrl
          })
        }
        // 合格证
        if (aaa == 8) {
          that.setData({
            hgz: urls,
            wuhgz: data.imgUrl
          })
        }
        // 采购索证
        if (aaa == 9) {
          that.setData({
            cgsz: urls,
            wucgsz: data.imgUrl
          })
        }
      })
    })
  },
  cancelBtn() {
    console.log('点击了放大！')
    wx.previewImage({
      urls: [this.data.fdimg]
    })
  },
  // 验证
  ti() {
    if (this.data.sbxh == '') {
      wx.showToast({
        title: '请输入设备型号',
        icon: 'none'
      })
    } else if (this.data.ssqy == '') {
      wx.showToast({
        title: '请输入所属企业',
        icon: 'none'
      })
    } else if (this.data.scdate == '请选择生产日期') {
      wx.showToast({
        title: '请选择生产日期',
        icon: 'none'
      })
    } else if (this.data.bfdate == '请选择报废日期') {
      wx.showToast({
        title: '请选择报废日期',
        icon: 'none'
      })
    } else if (this.data.wuscszz == '') {
      wx.showToast({
        title: '请上传生厂商资质',
        icon: 'none'
      })
    } else if (this.data.wuscxkz == '') {
      wx.showToast({
        title: '请上传生厂许可证',
        icon: 'none'
      })
    } else if (this.data.wujcbg == '') {
      wx.showToast({
        title: '请上传检查报告',
        icon: 'none'
      })
    } else if (this.data.wuhgz == '') {
      wx.showToast({
        title: '请上传合格证',
        icon: 'none'
      })
    } else if (this.data.wucgsz == '') {
      wx.showToast({
        title: '请上传采购索证',
        icon: 'none'
      })
    } else {
      this.que()
    }
  },
  // 添加设备
  que() {
    let that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'Gasstation/gasstation_add', {
      shopId: wx.getStorageSync('shopId'),
      models: that.data.sbxh,
      danwei: that.data.ssqy,
      sctime: that.data.scdate,
      bftime: that.data.bfdate,
      changshang: that.data.wuscszz,
      xuke: that.data.wuscxkz,
      jiancha: that.data.wujcbg,
      hege: that.data.wuhgz,
      caigou: that.data.wucgsz,
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.navigateBack({
            delta: 0
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
})